<?php

    class Cicp_InputProcessor
{
    private $_inputString = '';
    private $_processors = array();

    function preprocessInput($inputParameter)
    {
        $input = $inputParameter;
        // call preprocessors
        foreach ($this->_processors as $processor) {
            $processor->parseInput($input);
        }
        return $input;
    }

    function processCicpLine($cicpLine)
    {
        $this->_inputString = $this->preprocessInput($cicpLine);

        // parse string
        $parameters = explode(',', $this->_inputString);
        $type = $parameters[0];

        switch ($type) {
        case 'init':
            $this->initGraph($parameters[1], $parameters[2], $parameters[3]);
            break;
            
        default:
            $this->menuChoice($parameters);
        }

        return TRUE;
    }

    function initGraph($x, $y, $z)
    {
        // call user func that returns a graph object
        $graph = Cicp::getInstance()->getUserHandler()
            ->initGraph($x, $y, $z);

        // add create command to output
        Cicp::getInstance()->getOutputProcessor()->createGraph($graph);
    }

    function menuChoice($parameters)
    { 
        // load graph
        $graphId = $parameters[0];
        $graph = Cicp::getInstance()->getGraphManager()->getGraph($graphId);
        // set menu selected flag
        $graph->menuSelect = true;

        // get menu
        $node = $graph->getNodeById($parameters[1]);
        $menu = $node->getMenu();
        $buttonLabel = $menu->getButtonLabelByOptionId($parameters[2]);

        // call user func
        $userGraph = Cicp::getInstance()->getUserHandler()->menuChoice($graph, $node, $menu, $buttonLabel);
        Cicp::getInstance()->getOutputProcessor()->createGraph($userGraph);
    }
}